package io.github.harmonly.controller.api;

import io.github.harmonly.entity.Apply;
import io.github.harmonly.entity.Finance;
import io.github.harmonly.entity.activity.Activity;
import io.github.harmonly.entity.association.Association;
import io.github.harmonly.entity.association.Member;
import io.github.harmonly.service.*;
import io.github.harmonly.util.JSONUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Controller
@RequestMapping("/api")
public class ApplyController {

    @Resource
    private ApplyService service;
    @Resource
    private AssociationService associationService;
    @Resource
    private ActivityService activityService;
    @Resource
    private FinanceService financeService;
    @Resource
    private MemberService memberService;

    @ResponseBody
    @RequestMapping(value = "/apply/{apid}", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)
    public String getApply(@PathVariable("apid") int apid) {
        return JSONUtil.toJSONString(service.getApply(apid));
    }

    @ResponseBody
    @RequestMapping(value = "/applies", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)
    public String getApplies() {
        return JSONUtil.toJSONString(service.getApplies());
    }

    @ResponseBody
    @RequestMapping(value = "/applies/sender", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)
    public String getAppliesBySender(int uid) {
        return JSONUtil.toJSONString(service.getAppliesBySender(uid));
    }

    @ResponseBody
    @RequestMapping(value = "/applies/receiver", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)
    public String getAppliesByReceiver(int uid) {
        return JSONUtil.toJSONString(service.getAppliesByReceiver(uid));
    }

    @ResponseBody
    @RequestMapping(value = "/apply", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.POST)
    public String addApply(@RequestBody String json) {
        return JSONUtil.result(service.addApply(JSONUtil.parseObject(json, Apply.class)));
    }

    @ResponseBody
    @RequestMapping(value = "/apply/{apid}", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.DELETE)
    public String deleteApply(@PathVariable("apid") int apid) {
        return JSONUtil.result(service.deleteApply(apid));
    }

    @ResponseBody
    @RequestMapping(value = "/apply", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.PUT)
    public String editActivity(@RequestBody String json) {
        Apply apply = JSONUtil.parseObject(json, Apply.class);
        boolean result = service.editApply(apply);
        if (result && apply != null && apply.getState().equals("success")) {
            switch (apply.getType()) {
                case "association": {
                    Association association = JSONUtil.parseObject(apply.getContent(), Association.class);
                    associationService.addAssociation(association);
                    break;
                }
                case "activity": {
                    Activity activity = JSONUtil.parseObject(apply.getContent(), Activity.class);
                    activityService.addActivity(activity);
                    break;
                }
                case "finance": {
                    Finance finance = JSONUtil.parseObject(apply.getContent(), Finance.class);
                    if (finance == null) return JSONUtil.result(false);
                    Association association = associationService.getAssociation(finance.getAid());
                    if (association.getMoney() >= finance.getMoney()) {
                        association.setMoney(association.getMoney() - finance.getMoney());
                        if (associationService.editAssociation(association))
                            financeService.addFinance(finance);
                    }
                    break;
                }
                case "member": {
                    Member member = JSONUtil.parseObject(apply.getContent(), Member.class);
                    memberService.addMember(member);
                    break;
                }
                case "association-money": {
                    Finance finance = JSONUtil.parseObject(apply.getContent(), Finance.class);
                    if (finance == null) return JSONUtil.result(false);
                    Association association = associationService.getAssociation(finance.getAid());
                    association.setMoney(association.getMoney() + finance.getMoney());
                    if (associationService.editAssociation(association))
                        financeService.addFinance(finance);
                }
            }
        }
        return JSONUtil.result(result);
    }
}
